<?php

declare (strict_types=1);

namespace app\controller\admin;

use think\facade\View;
use think\facade\Config;
use think\facade\Request;

class Setting extends Common
{
    public function index($act = null)
    {
        if ($act == 'update') {
            if (!Request::isPost()) {
                return error(lang('参数错误'));
            }
            $data = input('post.');
            if (!isset($data['custom'])) {
                $data['custom'] = [];
            }
            $config_file = 'config/cy.php';
            if (!is_writable($config_file)) {
                return error(lang('请确保config/cy.php文件具有可读写权限'));
            }

            $result = file_put_contents($config_file, "<?php\r\nreturn " . var_export($data, true) . ";");
            if ($result) {
                add_log('修改网站配置。', $this->request->user->username);
                return success(lang('操作成功'), (string) url('admin/setting/index'));
            } else {
                return error(lang('参数错误'));
            }
        }

        return View::fetch('form');
    }
}
